package rwi.informationsystem.internal.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import rwi.informationsystem.core.interfaces.IInformationSystem;
import rwi.informationsystem.core.variables.RwiCommunication;

public class RWI_Servlet_register extends HttpServlet {
	/**
	 * 
	 */
	private IInformationSystem is;

	public RWI_Servlet_register(IInformationSystem is) {
		super();
		this.is = is;		
	}

	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		if (!req.getParameter("action").isEmpty()) {
			if ((req.getParameter("action")
					.equals(RwiCommunication.ACTION_REGISTER))) {
				// registering new Item
				int id;
				if (req.getParameter(RwiCommunication.PARAMETER_POSX)!=null && req.getParameter(RwiCommunication.PARAMETER_POSY)!=null && req.getParameter(RwiCommunication.PARAMETER_TYPE)!=null) {
					// if position is send
					int posX = Integer.parseInt(req.getParameter(RwiCommunication.PARAMETER_POSX));
					int posY = Integer.parseInt(req.getParameter(RwiCommunication.PARAMETER_POSY));
					int type = Integer.parseInt(req.getParameter(RwiCommunication.PARAMETER_TYPE));
					id = is.registerRWI_Object(type,posX, posY);
					resp.getWriter().write("" + id);
				} else {
					System.out.println("Registration Error");
				}
			} else if ((req.getParameter("action")
					.equals(RwiCommunication.ACTION_SHOW_IDS))) {
				// just show ids
				String s = is.getIds();
				resp.setContentType("text/html;charset=UTF-8");
				resp.getWriter().write(s);
			} else if ((req.getParameter("action")
					.equals(RwiCommunication.ACTION_UPDATE_POSITION))) {
				// update objects position
				if (req.getParameter(RwiCommunication.PARAMETER_POSX)!=null && req.getParameter(RwiCommunication.PARAMETER_POSY)!=null && req.getParameter(RwiCommunication.PARAMETER_ID)!=null) {
					// if position is send
					int id = Integer.parseInt(req.getParameter(RwiCommunication.PARAMETER_ID));
					int posX = Integer.parseInt(req.getParameter(RwiCommunication.PARAMETER_POSX));
					int posY = Integer.parseInt(req.getParameter(RwiCommunication.PARAMETER_POSY));
					is.updateRWIObjectPos(id, posX, posY);
					resp.getWriter().write("done");
				} else {
					resp.getWriter().write("update_error");
				}
			} else if ((req.getParameter("action")
					.equals(RwiCommunication.ACTION_UNREGISTER))) {
				// unregister object
				if (req.getParameter(RwiCommunication.PARAMETER_ID)!=null) {
					int id = Integer.parseInt(req.getParameter(RwiCommunication.PARAMETER_ID));
					is.unregisterRWI_Object(id);
					resp.getWriter().write("done");
				} else {
					resp.getWriter().write("unregister_error");
				}
			}else{
				resp.getWriter().write("command not known");
			}
		}
	}
}
